/***
 *
 * 登录界面和注册界面的切换
 */
const onland = document.querySelector(".land");
const onregist = document.querySelector(".regist");
const formEle = document.querySelector("form");
const h3Ele = document.querySelectorAll("h3");
function onTagel() {
  onland.addEventListener("click", function () {
    let landstr = `
           <input type="text" name="username" placeholder="请输入用户名"><br>
           <p class="nametix"></p>
            <input type="password" name="psw" placeholder="请输入密码"><br>
            <p class="psw"></p>
            <input type="submit" value="登录" class="submit">
               `;
    onClear();
    formEle.innerHTML = landstr;
    this.classList.add("active");
  });
  onregist.addEventListener("click", function () {
    let registstr = `
          <input type="text" name="username" placeholder="请输入要注册的用户名"><br>
          <p class="nametix"></p>
          <input type="password" name="psw" placeholder="密码长度必须大于8位且大写字母开头和数字结合"><br>
          <p class="psw"></p>
          <input type="submit" value="注册" class="submit">
            `;
    onClear();
    formEle.innerHTML = registstr;
    this.classList.add("active");
  });
}
/**
 * 清除效果
 *
 * */
function onClear() {
  for (let i = 0; i < h3Ele.length; i++) {
    h3Ele[i].classList.remove("active");
  }
}
/**
 * 注册功能实现
 *
 * 密码强度验证
 * 调用接口
 */
function OnRegist() {
  onTagel();
  formEle.addEventListener("submit", function (e) {
    e = e || window.event;
    e.preventDefault();
    const nameInput = document.querySelector('input[name="username"]');
    const pTix = document.querySelector(".nametix");
    const pswInput = document.querySelector('input[name="psw"]');
    const pPsw = document.querySelector(".psw");
    /**
 * 注册用户名非空验证
 */
    if (nameInput.value == "") {
      pTix.innerHTML = "请输入用户名";
      return;
    } else {
      pTix.innerHTML = "";
    }
    /**
     * 注册密码的非空验证
     */
    if (pswInput.value == "") {
      pPsw.innerHTML = "请输入密码";
      return;
    } else {
      pPsw.innerHTML = "";
    }
    /**
     * 密码强度验证
     */
     let username=nameInput.value
    let password=pswInput.value
     const reg = /^[A-Z][0-9a-zA-Z]{7,}/
     let isOk = reg.test(password)
     if (isOk==false) {
      pPsw.innerHTML = "密码强度不够";
       return
     }else {
      pPsw.innerHTML = "";
    }
   /**
    * 获取表单值
    * 调取接口
    */
    
    console.log(password);
    axios({
          method:'post',
          url:'http://api.yuguoxy.com/api/member/add',
          data:{
            username,
            password, 
          }
    }).then(response=>{
           let {resultCode,resultInfo}=response.data
           if(resultCode==1){
                 alert(resultInfo)
           }
    })
    
  });
}
/**
 * 登陆功能
 */
function onLand() {
      formEle.addEventListener("submit", function (e) {
        e = e || window.event;
        e.preventDefault();
        const nameInput = document.querySelector('input[name="username"]');
        const pTix = document.querySelector(".nametix");
        const pswInput = document.querySelector('input[name="psw"]');
        const pPsw = document.querySelector(".psw");
        console.log(nameInput);
        /**
     * 登陆用户名非空验证
     */
        if (nameInput.value == "") {
          pTix.innerHTML = "请输入用户名";
          return;
        } else {
          pTix.innerHTML = "";
        }
        /**
         * 登陆密码的非空验证
         */
        if (pswInput.value == "") {
          pPsw.innerHTML = "请输入密码";
          return;
        } else {
          pPsw.innerHTML = "";
        }
        
       /**
        * 获取表单值
        * 调取接口
        */
        let username=nameInput.value
        let password=pswInput.value
        console.log(password);
        axios({
              method:'post',
              url:'http://api.yuguoxy.com/api/member/login',
              data:{
                username,
                password, 
              }
        }).then(response=>{
               let {resultCode,resultInfo}=response.data
               if(resultCode==1){
                     let usernameListstr=JSON.stringify(resultInfo)
                     localStorage.setItem('userlist',usernameListstr)
                     location.href='../pages/index.html'
               }
        })
        
      }); 
}
onLand()
// console.log(getNameNull);
OnRegist();
onTagel();
